﻿import axios from 'axios'

// import {Loading} from 'element-ui'
// var loadingInstance;
var timeout;

// import showAlert from '@/common/js/errorHandle'

//login session 失效

import showLogin from '@/common/js/loginPop'
// import showLogin from '@/common/js/loginPanel'

import showLoading from '@/common/js/loading'

// import '@/common/js/vconsole.min.js'
// setTimeout(()=>{
//     new VConsole()
// },2000);

// import Vue from 'vue';
// import infoDialog from '@/components/public/alterUser.vue'
// const Pop = Vue.extend(infoDialog);
// setTimeout(()=>{
//   new Pop({
//       el: document.createElement('div'),
//       data:{
//       }
//   })
//   document.body.appendChild((new Pop({
//     el: document.createElement('div'),
//     data:{
//     }
// })).$el);
// document.getElementById('select').value = localStorage.getItem('userid') || '';
// },2000)

// window.onload=function(){
//     document.getElementById('select').value = localStorage.getItem('userid') || '';
// }

export default{
    // baseURI:"http:\/\/" +  window.location.hostname,
    // baseURI:window.location.origin,
    loadingInstance:null,
    baseURI: function(){  
        if (window.location.hostname == "10.5.0.137" || window.location.hostname == '10.16.3.104' || window.location.hostname == '202.104.142.49') {
            return window.location.origin;
        }
        return ("http:\/\/" +  window.location.hostname);           
    }(),
    // baseURI: "http://10.5.0.137:8080",
    // baseURI: "http://10.5.0.137:9080",
    // baseURI: "http://192.168.100.156:8080",
    // baseURI: "http://10.8.3.100:80",
    // baseURI:'http://10.8.253.72:8081',
    // baseURI: "http://192.168.0.5:19080",
	Post : function(url, params, method, showLoading){

		//发送ajax请求
		return axios.request({
			method: method || 'POST',
			url : url,
			//params : params,微服务参数放在了url后
			// params:params,
			baseURL: this.baseURI,

			responseType: 'json',
			headers : {
                "Content-Type": "application/json"
			},
            data:params,
			transformRequest : function(data){

                if (showLoading) {
                    showLoading.showLoginPanel()
                }

                data = data ? data : {};
                var token = localStorage.getItem("token");
                data.token = token ? token : '';
                return JSON.stringify(data);
			},
			transformResponse : function(response){
				// 这里提前处理返回的数据
        
				// if(response&&response.status&&response.status==='01'){
				// 	window.localStorage.removeItem('hyoaTK');
				// 	window.location.href='/module/login.html';
                // }
                
                if (showLoading) {
                    showLoading.destroyInstance();
                }
                
                
                typeof(response)==='string' && (response = JSON.parse(response)) 
                
                if (response.status != 1 && response.status != 200 ) {
                    var message = response.message;
            
                    if (message.indexOf('token') !== -1 || message.indexOf('网络异常') !== -1 ) {
                        showLogin.showLoginPanel();
                    }  
                }   
                // }

                // window.clearTimeout(timeout);
                // timeout = null;
                // loadingInstance && loadingInstance.close();
                // loadingInstance = null;
				return response;
			},
			validateStatus: function (state) {
				//请求返回状态

    			return state >= 200 && state < 300;
			},
            withCredentials: true
		});
	},
    StringPost:function(url,params){
    		//发送ajax请求
		return axios.request({
			method:  'POST',
			url : url,
			//params : params,微服务参数放在了url后
			// params:params,
			baseURL: this.baseURI,
			// params:params,
			
			timeout: 15000,
			responseType: 'json',
			headers : {
                "Content-Type": "application/json"
			},
            data:params,
			transformRequest : function(data){
                // let ret = ''
		        // for (let it in data) {
		        //   ret += it + '=' + data[it] + '&'
		        // }
                
                // return ret;
                data = data ? data : {};
                var token = localStorage.getItem("token");
                data.token = token ? token : '';
                return JSON.stringify(data);
                
			},
			transformResponse : function(response){
				// 这里提前处理返回的数据
                typeof(response)==='string' && (response = JSON.parse(response)) 
                
                if (response.status !== 1 && response.status !== 200) {
                    var message = response.message;
       
                    if (message.indexOf('token') !== -1 || message.indexOf('网络异常') !== -1 ) {
                        showLogin.showLoginPanel();
                    }  
                }   
                
				return response;
			},
			validateStatus: function (state) {
				//请求返回状态

    			return state >= 200 && state < 300;
			},
            withCredentials: true
		});    
    },
    //以FormData形式发送POST请求，可用来上传文件
    axiosPostFormData : function(url,form,type){
        //发送ajax请求
        if (type) {
            url = url + "?token=" + localStorage.getItem('token');
        }
        return axios.post(
        url,
        form,
        {
            method: 'post',
            baseURL: this.baseURI,
            headers: {"Content-Type": "multipart/form-data"},
            transformRequest : function(data){
            // 这里可以在发送请求之前对请求数据做处理，比如form-data格式化等，这里可以使用开头引入的Qs（这个模块在安装axios的时候就已经安装了，不需要另外安装）
                if (!type) {
                    var token = localStorage.getItem("token");
                    data.token = token ? token : '';
                }

                            
                if(data && data instanceof FormData){
                }else{
                    let formdata = new FormData();
                    let i = 0;
                    for (let it in data) {
                        if (it === 'fileList') {
                            data[it].forEach((item) => {
                                formdata.append('file', item.file);
                            })
                        } else{
                            formdata.append(it, data[it]);
                        }
                        
                    }



                    return formdata;
                }
            },
            transformResponse : function(response){
                // 这里提前处理返回的数据
                // if(response&&response.status&&response.status==='01'){
                //     window.localStorage.removeItem('hyoaTK');
                //     window.location.href='/module/login.html';
                // }
                
                response = typeof response === 'string' ? JSON.parse(response) : response;
                console.log(response,"response");
                if (response.status != 1 && response.status != 200) {
                    var message = response.message;
                    console.log(message,"message");
                    if (message.indexOf('token') !== -1 || message.indexOf('网络异常') !== -1 ) {
                        showLogin.showLoginPanel();
                    }  
                }   
                
                return response;
            }
        }
        );
    }
}
